package com.hmkj.admin.controller.holiday;

import com.hmkj.admin.controller.BaseController;
import com.hmkj.admin.controller.GenericController;
import com.hmkj.admin.utils.AdminSessionHelper;
import com.hmkj.common.entity.ResultEntity;
import com.hmkj.common.entity.dtgrid.DtGrid;
import com.hmkj.common.utils.StringUtils;
import com.hmkj.core.exception.BussinessException;
import com.hmkj.core.po.holiday.Holiday;
import com.hmkj.core.service.holiday.HolidayService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;

@Controller
@RequestMapping(BaseController.BASE_URI)
public class HolidayJsonController extends GenericController<Integer, Holiday> {
    @Resource
    private HolidayService holidayService;

    /**
     * 节假日
     *
     * @param dtGridPager
     * @return
     */
    @ResponseBody
    @RequestMapping("holiday/json/list")
    public DtGrid userListJson(String dtGridPager) throws Exception {
        return holidayService.getDtGridList(dtGridPager, Holiday.class);
    }

    /**
     * 新增
     *
     * @param holiday
     * @return
     * @throws Exception
     */
    @ResponseBody
    @RequestMapping("holiday/json/add")
    @RequiresPermissions("sys:holiday:save")
    public ResultEntity addJson(Holiday holiday) throws Exception {
        checkModel(holiday);
        if (holidayService.selectCount(new Holiday(t -> {
            t.setHoliday(holiday.getHoliday());
            t.setDelFlag(Holiday.DELFLAG.NORMAL.code);
        }))>0 || holidayService.selectCount(new Holiday(t -> {
            t.setRemark(holiday.getRemark());
            t.setDelFlag(Holiday.DELFLAG.NORMAL.code);
        }))>0) {
            throw new BussinessException("节日已存在");
        }
        holiday.setCreator(AdminSessionHelper.getCurrAdmin().getName());
        holidayService.insert(holiday);
        return ok();
    }
    /**
     * 编辑
     */
    @ResponseBody
    @RequestMapping("holiday/json/edit")
    @RequiresPermissions("sys:holiday:edit")
    public ResultEntity editJson(Holiday holiday) throws Exception {
        checkModel(holiday);
        Holiday holiday1=checkPrimaryKey(holiday.getId());
        if (!holiday1.getHoliday().equals(holiday.getHoliday()) || !holiday1.getRemark().equals(holiday.getRemark())){
            if (holidayService.selectCount(new Holiday(t -> {
                t.setHoliday(holiday.getHoliday());
            }))>0 && holidayService.selectCount(new Holiday(t -> {
                t.setRemark(holiday.getRemark());
            }))>0){
                throw new BussinessException("日期或备注已存在");
            }
            holiday.setUpdator(AdminSessionHelper.getCurrAdmin().getName());
            holidayService.updateByPrimaryKeySelective(holiday);
        }
        return ok();
    }
    /**
     * 删除
     *
     * @param id
     * @return
     * @throws Exception
     */
    @ResponseBody
    @RequestMapping(value = "holiday/json/del")
    @RequiresPermissions("sys:holiday:del")
    public ResultEntity del(Integer id) throws Exception {
        Holiday holiday=checkPrimaryKey(id);
        holidayService.updateByPrimaryKeySelective(new Holiday(a -> {
            a.setId(holiday.getId());
            a.setDelFlag(Holiday.DELFLAG.DELETED.code);
        }));
        return ok();
    }

    private void checkModel(Holiday holiday) {
        if (StringUtils.isBlank(holiday.getHoliday())) {
            throw new BussinessException("日期不能为空");
        } else if (holiday.getHoliday().length() > 8) {
            throw new BussinessException("日期长度不能超过8位");
        } else if (StringUtils.isBlank(holiday.getRemark())) {
            throw new BussinessException("备注不能为空");
        } else if (holiday.getRemark().length() > 50) {
            throw new BussinessException("备注长度不能超过50位");
        }
    }

    private Holiday checkPrimaryKey(Integer id) throws Exception {
        if (null == id) {
            throw new BussinessException("参数有误");
        } else {
            Holiday holiday = holidayService.selectOne(new Holiday(t -> {
                t.setId(id);
                t.setDelFlag(Holiday.DELFLAG.NORMAL.code);
            }));
            if (null != holiday) {
                return holiday;
            } else {
                throw new BussinessException("参数有误");
            }
        }
    }
}
